package edu.sctu;

import java.sql.Connection;
import java.sql.DriverManager;


public class DbUtil {

    private String dbUrl= "jdbc:mysql://localhost:3306/sctu_javaweb_2018?characterEncoding=utf-8&useUnicode=true&useJDBCCompliantTimezoneShift=true&useLegacyDatetimeCode=false&serverTimezone=UTC";
    private String dbUserName="root";
    private String dbPassword="123456";
    private String jdbcName="com.mysql.jdbc.Driver";

    //获取数据库连接
    public Connection getCon()throws Exception{
        Class.forName(jdbcName);

        Connection connection = DriverManager.getConnection(dbUrl,dbUserName,dbPassword);
        return connection;
    }

    //关闭连接
    public void closeCon(Connection connection)throws Exception{
        if (connection!=null){
            connection.close();
        }
    }

    //测试数据库连接
    public static void main(String[] args) {
        DbUtil dbUtil = new DbUtil();
        try {
            dbUtil.getCon();
            System.out.println("数据库连接成功");
        } catch (Exception e) {
            e.printStackTrace();
            System.out.println("数据库连接失败");
        }
    }


}
